package com.chushouya.product.dao.repository;

import com.general.framework.core.lang.Lists;
import com.general.framework.data.mybatis.BaseRepository;
import com.chushouya.product.dao.entity.ProductSeriesEntity;
import com.chushouya.product.dao.mapper.ProductSeriesEntityMapper;
import com.chushouya.product.dto.admin.series.SeriesQuery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Objects;

/**
 * 商品系列Repository
 */
@Repository
@Slf4j
public class ProductSeriesRepository extends BaseRepository<ProductSeriesEntityMapper, ProductSeriesEntity, Long> {

    public List<ProductSeriesEntity> selectList(SeriesQuery query) {
        log.info("====>selectList query:{}", query);
        final Example example = this.buildExample();
        if (Objects.nonNull(query.getCategoryId())) {
            example.and().andEqualTo("categoryId", query.getCategoryId());
        }
        if (Lists.isNotEmpty(query.getCategoryIdList())) {
            example.and().andIn("categoryId", query.getCategoryIdList());
        }
        if (Objects.nonNull(query.getBrandId())) {
            example.and().andEqualTo("brandId", query.getBrandId());
        }
        if (Lists.isNotEmpty(query.getBrandIdList())) {
            example.and().andIn("brandId", query.getBrandIdList());
        }
        if (Objects.nonNull(query.getStatus())) {
            example.and().andEqualTo("status", query.getStatus());
        }
        example.orderBy("sort").asc().orderBy("seriesId").asc();
        return this.selectByExample(example);
    }
}
